// pages/gmlist/gmlist.js
const app = getApp();
const url = app.globalData.url;
const common = require("../../utils/common.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    jzs:false,
    loding:true,
    list:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.peizi();
  },
   // 配置文件
   peizi: function () {
    let that = this;
    app.fetch('api/wx/getpeizi', { dlx: 'xcxadd' }, "GET", (err, data) => {
      console.log(data)
      if (data.code == 200) {
        
        that.setData({
          peizi: data.data,
        });
        
        that.hquser();
      } else {
        wx.showModal({
          title: '接口数据错误',
          content: '接口:"' + url + 'api/wx/getpeizi.php' + '",msg:' + data.msg,
          showCancel: false,//是否显示取消按钮
          confirmText: "好的",//默认是“确定”
          confirmColor: 'skyblue',//确定文字的颜色
          success: function (res) {

          },
          fail: function (res) { },//接口调用失败的回调函数
          complete: function (res) { },//接口调用结束的回调函数（调用成功、失败都会执行）
        })
      }
    })
  },
  // 获取user信息;
  hquser() {
    let that = this;
    wx.getStorage({
      key: 'openid',
      success(res) {
        app.fetch('api/wx/user', { openid: res.data }, "POST", (err, data) => {
          if (data.code == 200) {
            that.setData({ user: data.data[0]})
            that.hqgmlist();
          }
        })
      },
      fail(){
        wx.showModal({
          title: '提示',
          content: '请先登录',
          success(res){
            if (res.cancel) {
              //点击取消,默认隐藏弹框
              // if (options.finish) {
                wx.navigateBack({ delta: 1 })
              // }
            } else {
                wx.redirectTo({
                  url: "/pages/my/my"
                })
            }
          }
        })
      }
    })
  },
  tourl(e) {
    let url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url
    })
  },
  hqgmlist(sx){
    let that=this;
    let list = []
    let pno = 1;
    if (sx) {
      list = that.data.list;
      pno = that.data.pno;
    }
    let sj={
      uid:that.data.user.uid,
      pno:pno,
      pgs:10
    };
    app.fetch('api/wx/getgmb', sj, "POST", (err, data) => {
      wx.hideLoading();
      console.log(data)
      if(data.code==200){
        for (var i = 0; i < data.list.length; i++) {
          if (data.list[i].detail.ewm) {
            data.list[i].detail.ewm = url + data.list[i].detail.ewm;
          }
          data.list[i].detail.addtime = common.bijiao(data.list[i].detail.addtime);
          if (data.list[i].detail.logolist) {
            var arr = data.list[i].detail.logolist.split(",");
            data.list[i].detail.logolist = [];
            let ls = arr.length < 5 ? arr.length : 5;
            for (let a = 0; a < ls; a++) {
              data.list[i].detail.logolist.push(url +arr[a]);
            }
          } else {
            data.list[i].detail.logolist = [];
            // data[i].imgs.push(data[i].tx);
          };
          list.push(data.list[i]);
          wx.hideLoading();
        }
        that.setData({
          list: list,
          pno: Number(data.pno) + 1,
          pcount: Number(data.pageCount), 
          loding: false 
        });

      }else if(data.code==400){
        wx.showModal({
          title: '提示',
          content: '请求内容为空' + '",msg:' + data.msg,
          showCancel: true,//是否显示取消按钮
          confirmText: "返回",//默认是“确定”
          confirmColor: 'skyblue',//确定文字的颜色
          success: function (res) {
            if (res.cancel) {
              //点击取消,默认隐藏弹框
              // if (options.finish) {
              // wx.navigateBack({ delta: 1 })
              // }
              that.setData({loding:false})
            } else {
              wx.navigateBack({
                data: 1,
                fail() {
                  wx.redirectTo({
                    url: '/pages/index/index'
                  })
                }
              })
            }
          },
        })
      }else if(data.code==500){
        wx.showModal({
          title: '接口数据错误',
          content: '接口:"' + url + 'api/wx/getgmb' + '",msg:' + data.msg,
          showCancel: false,//是否显示取消按钮
          confirmText: "好的",//默认是“确定”
          confirmColor: 'skyblue',//确定文字的颜色
          success: function (res) {
            wx.navigateBack({
              data: 1,
              fail(){
                wx.redirectTo({
                  url: '/pages/index/index'
                })
              }
            })
          },
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
      var count = that.data.pcount;
      var pno = that.data.pno;
      console.log(pno, count)
      if (pno - 1 < count) {
        wx.showLoading({
          title: "正在加载中....",
          // mask: true
        });
        // that.hqlist('sx');
        that.hqgmlist('sx');
      } else {
        that.setData({ jzs: true });
        setTimeout(function () {
          that.setData({ jzs: false });
        }, 3000)
      }
  },

  /**
  //  * 用户点击右上角分享
  //  */
  // onShareAppMessage: function () {

  // }
})